﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Services;
using Foothill.Common;
using Foothill.Common.Logging;
using Foothill.IoC;
using Foothill.Model;
using Foothill.Service.Session;
using Foothill.Service.SystemMessage;
using Foothill.Service.UploadPhoto;
using Foothill.Service.Configuration;

namespace Foothill.Web
{
    public partial class UploadPhotoAsync : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IHostSettingManager hostSettingManager = IocContainer.GetInstance<IHostSettingManager>();
                lblNextStepUrl.Text = hostSettingManager.GetSiteBaseUrlString() + "/PostForSale/PostForSalePreview.aspx";
            }
        }

        [WebMethod]
        public static void ClearCurrentPhotoListInSession()
        {
            ISessionService sessionService = IocContainer.GetInstance<ISessionService>();
            sessionService.ClearSessionVariable(SessionNames.ForSalePhotoList);
        }

        [WebMethod]
        public static void DeleteUploadedPhoto(string filenameNoExt)
        {
            if (!string.IsNullOrWhiteSpace(filenameNoExt))
            {
                IList<UploadPhoto> uploadedPhotos = GetUploadedPhotoListFromSession();
                UploadPhoto fileToDelete = uploadedPhotos.FirstOrDefault(p => string.Compare(Path.GetFileNameWithoutExtension(p.PathOnServer), filenameNoExt, true) == 0);

                if (fileToDelete != null)
                {
                    IUploadPhotoService uploadPhotoService = IocContainer.GetInstance<IUploadPhotoService>();
                   
                    try
                    {
                        // delete physical files - photo and thumbnail
                        uploadPhotoService.DeletePhotoAndThumbnail(fileToDelete.PathOnServer, fileToDelete.PathOnServer);
                    }
                    catch (Exception ex)
                    {
                        Log.Error(FoothillModule.View, "Fail to delete a photo from the disk", ex);
                    }

                    uploadedPhotos.Remove(fileToDelete);

                    ISessionService sessionService = IocContainer.GetInstance<ISessionService>();
                    sessionService.SaveSessionValue(SessionNames.ForSalePhotoList, uploadedPhotos);
                }
            }
        }

        [WebMethod]
        public static void Submit(List<string> list)
        {
            IList<UploadPhoto> photoList = GetUploadedPhotoListFromSession();

            foreach (UploadPhoto uploadPhoto in photoList)
            {
                int index = list.FindIndex(x => x.Equals(uploadPhoto.PathOnServer));
                uploadPhoto.DisplayIndex = index;
            }

            ISessionService sessionService = IocContainer.GetInstance<ISessionService>();
            sessionService.SaveSessionValue(SessionNames.ForSalePhotoList, photoList);
        }

        // get the photo list from the session
        private static IList<UploadPhoto> GetUploadedPhotoListFromSession()
        {
            List<UploadPhoto> retVal;
            ISessionService sessionService = IocContainer.GetInstance<ISessionService>();
            object obj = sessionService.GetSessionValue(SessionNames.ForSalePhotoList);

            if (obj == null)
            {
                // no photo has been uploaded yet
                retVal = new List<UploadPhoto>();
            }
            else
            {
                retVal = obj as List<UploadPhoto>;
            }

            return retVal;
        }
    }
}